/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.charts.SeriesLabel;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewsample.FontSourceProcessor;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;


public class Sample2Ability extends Ability {
    private int mSeries1Index;
    private int mSeries2Index;

    private DecoView decoView;
    private Text textProgress;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_2);

        decoView = (DecoView) findComponentById(ResourceTable.Id_decoView);
        textProgress = (Text) findComponentById(ResourceTable.Id_textProgress);

        createTracks();
        setupEvents();
    }

    private void createTracks() {
        decoView.deleteAll();
        decoView.configureAngles(280, 0);

        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(0, 0, 0));
        decoView.setBackground(element);

        final float seriesMax = 50f;
        decoView.addSeries(new SeriesItem.Builder(Color.argb(255, 218, 218, 218))
                .setRange(0, seriesMax, seriesMax)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(32f, this))
                .build());

        Font font = FontSourceProcessor.process(ResourceTable.Profile_dancing, this);

        SeriesItem seriesItem1 = new SeriesItem.Builder(Color.argb(255, 255, 0, 0))
                .setRange(0, seriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(32f, getApplicationContext()))
                .setSeriesLabel(new SeriesLabel.Builder("Percent %.0f%%")
                        .setColorBack(RgbPalette.parse("#FFFFFFFF"))
                        .setColorText(RgbPalette.parse("#FF000000"))
                        .setTypeface(font)
                        .build())
                .build();

        mSeries1Index = decoView.addSeries(seriesItem1);

        SeriesItem seriesItem2 = new SeriesItem.Builder(Color.argb(255, 0, 0, 255))
                .setRange(0, seriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(24f, getApplicationContext()))
                .setSpinDuration(3000)
                .setSeriesLabel(new SeriesLabel.Builder("Value %.0f").build())
                .build();

        mSeries2Index = decoView.addSeries(seriesItem2);

        textProgress.setVisibility(Component.INVISIBLE);
    }

    private void setupEvents() {
        decoView.executeReset();

        final Component[] linkedViews = {textProgress};

        decoView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_SHOW, true)
                .setDelay(500)
                .setDuration(2000)
                .setLinkedViews(linkedViews)
                .build());

        decoView.addEvent(new DecoEvent.Builder(25).setIndex(mSeries1Index).setDelay(3300).build());
        decoView.addEvent(new DecoEvent.Builder(50).setIndex(mSeries1Index).setDelay(8000).setDuration(1000).build());
        decoView.addEvent(new DecoEvent.Builder(0).setIndex(mSeries1Index).setDelay(13000).setDuration(6000).build());

        decoView.addEvent(new DecoEvent.Builder(5).setIndex(mSeries2Index).setDelay(4250).setDuration(2000).build());
        decoView.addEvent(new DecoEvent.Builder(30).setIndex(mSeries2Index).setDelay(9000).build());
        decoView.addEvent(new DecoEvent.Builder(0)
                .setIndex(mSeries2Index)
                .setDelay(13000)
                .build());

        decoView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_HIDE, false)
                .setDelay(19500)
                .setDuration(2000)
                .setLinkedViews(linkedViews)
                .setListener(new DecoEvent.ExecuteEventListener() {
                    @Override
                    public void onEventStart(DecoEvent event) {

                    }

                    @Override
                    public void onEventEnd(DecoEvent event) {
                    }
                })
                .build());
    }
}